//
//  ForgetPwdViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/2/28.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "UITextView+WZB.h"

@interface ForgetPwdViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong) UITextField *psw1;
@property (nonatomic,strong) UITextField *psw2;
@property (nonatomic,strong) UITextField *pswTipTF;
@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    self.title = XLocalized(@"忘记密码");
    
    UILabel *tipLab = [UILabel new];
    tipLab.font = kBoldFont(16);
    tipLab.textColor = UIColorHex(333333);
    tipLab.text = XLocalized(@"个人私钥");
    [self.contentView addSubview:tipLab];
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(kNormalSpace);
    }];
    self.textView = [UITextView new];
    self.textView.layer.cornerRadius = AdaptedWidth(10);
    self.textView.font = kDefaultFont(14);
    self.textView.backgroundColor = UIColorHex(#F7F7F7);
    self.textView.wzb_placeholder = XLocalized(@"请输入您的私钥");
    [self.contentView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(130));
        make.top.equalTo(tipLab.mas_bottom).offset(AdaptedHeight(18));
    }];
    UILabel *tipLab2 = [UILabel new];
    tipLab2.font = kBoldFont(16);
    tipLab2.textColor = UIColorHex(333333);
    tipLab2.text = XLocalized(@"新密码(不少于8位)");
    [self.contentView addSubview:tipLab2];
    [tipLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textView.mas_bottom).offset(AdaptedHeight(25));
        make.left.mas_equalTo(kNormalSpace);
    }];
    self.psw1 = [UITextField new];
    self.psw1.delegate = self;
    self.psw1.secureTextEntry = YES;
    self.psw1.borderStyle = UITextBorderStyleNone;
    self.psw1.layer.cornerRadius = AdaptedWidth(10);
    self.psw1.backgroundColor = UIColorHex(#F7F7F7);
    self.psw1.placeholder = XLocalized(@"请输入新密码");;
    self.psw1.font = kDefaultFont(14);
    UIView*letfV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, AdaptedWidth(30))];
    self.psw1.leftView = letfV;
    self.psw1.leftViewMode = UITextFieldViewModeAlways;
    [self.contentView addSubview:self.psw1];
    [self.psw1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipLab2.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    self.psw2 = [UITextField new];
    self.psw2.delegate = self;
    self.psw2.secureTextEntry = YES;
    self.psw2.borderStyle = UITextBorderStyleNone;
    self.psw2.layer.cornerRadius = AdaptedWidth(10);
    self.psw2.backgroundColor = UIColorHex(#F7F7F7);
    self.psw2.placeholder = XLocalized(@"请再次输入新密码");;
    self.psw2.font = kDefaultFont(14);
    UIView*letfV1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, AdaptedWidth(30))];
    self.psw2.leftView = letfV1;
    self.psw2.leftViewMode = UITextFieldViewModeAlways;
    [self.contentView addSubview:self.psw2];
    [self.psw2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.psw1.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    UILabel *tipLab3 = [UILabel new];
    tipLab3.font = kBoldFont(16);
    tipLab3.textColor = UIColorHex(333333);
    tipLab3.text = XLocalized(@"设置密码提示（非必填项）");
    [self.contentView addSubview:tipLab3];
    [tipLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.psw2.mas_bottom).offset(AdaptedHeight(25));
        make.left.mas_equalTo(kNormalSpace);
    }];
    self.pswTipTF = [UITextField new];
    self.pswTipTF.borderStyle = UITextBorderStyleNone;
    self.pswTipTF.layer.cornerRadius = AdaptedWidth(10);
    self.pswTipTF.backgroundColor = UIColorHex(#F7F7F7);
    self.pswTipTF.placeholder = XLocalized(@"请输入您的密码提示");;
    self.pswTipTF.font = kDefaultFont(14);
    UIView*letfV3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, AdaptedWidth(30))];
    self.pswTipTF.leftView = letfV3;
    self.pswTipTF.leftViewMode = UITextFieldViewModeAlways;
    [self.contentView addSubview:self.pswTipTF];
    [self.pswTipTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipLab3.mas_bottom).offset(AdaptedWidth(15));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    
    XThemeFlatButton*importBtn = [XThemeFlatButton button];
    [importBtn setTitle:XLocalized(@"完成") forState:UIControlStateNormal];
    [self.contentView addSubview:importBtn];
    [importBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pswTipTF.mas_bottom).offset(AdaptedHeight(67));
        make.left.mas_equalTo(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.height.mas_equalTo(AdaptedWidth(40));
        make.bottom.mas_equalTo(0);
    }];
    
    kWeakSelf(self);
    [importBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [weakself.view endEditing:YES];
        if (!weakself.textView.text.length) {
            [weakself.view makeToast:weakself.textView.wzb_placeholder];
            return;
        }
        if (!weakself.psw1.text.length) {
            [weakself.view makeToast:weakself.psw1.placeholder];
            return;
        }
        if (weakself.psw1.text.length<8) {
            [weakself.view makeToast:XLocalized(@"密码不少于8位!")];
            return;
        }
        if (!weakself.psw2.text.length) {
            [weakself.view makeToast:weakself.psw2.placeholder];
            return;
        }
        if (![weakself.psw1.text isEqualToString:weakself.psw2.text]) {
            [weakself.view makeToast:XLocalized(@"密码不一致!")];
            return;
        }
        NSString* publicKey = [EOS_Key_Encode eos_publicKey_with_wif:self.textView.text];
        if ([publicKey isEqualToString:weakself.wallet.wallet_public_key]) {
            weakself.wallet.wallet_private_AES_key = [WalletTool encryptWalletPrivatekey:weakself.textView.text password:weakself.psw1.text];
            weakself.wallet.password_sha = [weakself.psw1.text sha256String];
            weakself.wallet.password_tip = self.pswTipTF.text.length?self.pswTipTF.text:@"";
            [[XHDataBaseManager shareInstance]updateWalletToDB:weakself.wallet];
            [[WalletTool sharedWalletTool].walletUseFingerprint removeObjectForKey:weakself.wallet.wallet_uid];
            [kAppWindow makeToast:XLocalized(@"修改成功!")];
            [weakself backBtnClicked];
        }else{
            [weakself.view makeToast:XLocalized(@"无效的私钥地址，请检查是否抄写正确")];
        }
    }];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return [RegularExp controllABC123WithString:textField.text inRange:range inputString:string];
}
@end
